package com.project.config;

import com.project.common.entity.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @Description:
 * @Author: zhangqingbiao
 * @Date: 2021/3/31 0:58
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public CommonResult globalException(HttpServletResponse response, NullPointerException ex) {


        log.info("GlobalExceptionHandler...");
        log.info("错误代码：" + response.getStatus());
        /*WebResult webResult = new WebResult();
        CommonResult result = new WebResult(, "request error:" + response.getStatus()
                , "GlobalExceptionHandler:" + ex.getMessage());*/
        CommonResult<Object> objectCommonResult = new CommonResult<>();
        System.out.println(response.getStatus());
        objectCommonResult.setCode("1000");
        objectCommonResult.setInfo(ex.getMessage());

        return objectCommonResult;
    }

}